(function () {

    "use strict";

    var api = {
        listUrl: "/api/collect/lists",
        deleteUrl: '/api/collect/deleteItem'
    };
    sm.controller('wx-center-collect-index-controller', ['$scope', '$http', 'Toast', 'Modal', function ($scope, $http, Toast, Modal){

        $scope.lists = [];
        $scope.total = 0;
        $scope.selected = false;

        // 获取列表
        $scope.getCollectLists = function () {
            $http.get(api.listUrl, {
                page: 1
            }).success(function (res) {
                $scope.selected = true;
                if (res.state) {
                    // 列表
                    $scope.total = res.data.total;
                    $scope.lists = res.data.data;

                    angular.forEach($scope.lists, function (val) {
                        val['stars_arr'] = [];
                        for(var i = 0; i < val['stars']; i++) {
                            val['stars_arr'].push(i);
                        }
                    });

                }
            });
        };

        $scope.getCollectLists();

        $scope.deleteItem = function (item) {

            Modal.alert({
                title: '操作提示',
                'content': '您确认要移除此商品吗?',
                buttons: [{
                    name: '取消'
                }, {
                    name: '确认移除',
                    event: function () {
                        doRemove(item);
                    }
                }]
            });
        };

        function doRemove(item) {
            var index = $scope.lists.indexOf(item);
            Toast.loading();
            $http.post(api.deleteUrl, {
                goodsId: item['goodsId']
            }).success(function (res) {
                Toast.stop();
                if (res.state) {
                    $scope.lists.splice(index, 1);
                    Toast.success({title: '移除成功'});
                } else {
                    Toast.error({title: '移除失败'});
                }
            });
        }

    }]);
})();

